using System.IO;

namespace Kopernikus.Parsing.AST
{
    public class ASTProductionNode : ASTNode
    {
        private readonly ASTNode[] m_children;
        private readonly Production m_production;

        public ASTProductionNode(Production production, ASTNode[] children)
        {
            m_production = production;
            m_children = children;
        }

        public Production Production
        {
            get { return m_production; }
        }

        public override IConvertToSymbol Token
        {
            get { return Symbol; }
        }

        public override Symbol Symbol
        {
            get { return m_production.LHS; }
        }

        public override int ChildCount
        {
            get { return m_children.Length; }
        }

        public override ASTNode this[int index]
        {
            get { return m_children[index]; }
        }

        public override void PrettyPrint(TextWriter writer, int level)
        {
            foreach (ASTNode node in m_children)
            {
                if (node == null)
                {
                    writer.Write("(null) ");
                }
                else
                {
                    node.PrettyPrint(writer, level + 1);
                    writer.Write(' ');
                }
            }
        }
    }
}